//
//  UIButton+extension.swift
//  Weibo
//
//  Created by 孙明海 on 16/1/23.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import UIKit

extension UIButton {
    /// 遍历构造函数
    ///
    /// - parameter imageName:     图片名称
    /// - parameter backImageName: 背景图片名称
    convenience init(imageName: String, backImageName: String?) {
        self.init()
        setImage(UIImage(named: imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        if let backImageName = backImageName {
            
            setBackgroundImage(UIImage(named: backImageName ), forState: .Normal)
            setBackgroundImage(UIImage(named: backImageName  + "_highlighted"), forState: .Highlighted)
        }
        sizeToFit()
    }
    /// 便利构造函数
    ///
    /// - parameter title:         文字
    /// - parameter fontSize:      字体大小
    /// - parameter color:         颜色
    /// - parameter BackImageName: 背景图片
    /// - parameter backColor:     背景颜色(默认为nil)
    convenience init(title: String,fontSize: CGFloat, color: UIColor ,backImageName: String?, backColor: UIColor? = nil) {
        self.init()
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        if backImageName != nil {
            
            setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
        }
        backgroundColor = backColor
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        sizeToFit()
    }
    /// 遍历构造函数
    ///
    /// - parameter title:         文字
    /// - parameter color:         字体颜色
    /// - parameter backImageName: 背景图片名称
    /// - parameter backColor:     背景颜色
    convenience init(title: String, color: UIColor ,backImageName: String?, backColor: UIColor? = nil) {
        self.init()
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        if backImageName != nil {
            
            setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
        }
        if backColor != nil {
            backgroundColor = backColor
        }
    }
    /// 便利构造函数
    ///
    /// - parameter title:     文字
    /// - parameter imageName: 图片
    convenience init(title: String, imageName: String) {
        self.init()
        setTitle(title, forState: .Normal)
        setImage(UIImage(named: imageName), forState: .Normal)
        sizeToFit()
    }
}
